package ibatis;

import ibatis.config.SqlMapConfig;

import java.sql.SQLException;
import java.util.List;

import models.Times;

import com.ibatis.sqlmap.client.SqlMapClient;

public class Functions {

	public static boolean insertCheckIn(){
		try{
			SqlMapClient sqlMap = SqlMapConfig.getSqlMapInstance();	
			sqlMap.insert("insertCheckIn");
			return true;
		}catch(SQLException e){
			if(e.getMessage().contains("ORA-00001")){ //unique constraint violated
				System.out.println("You are allready checked in today");
			}else{			
				System.out.println("An unexpected error happends checking you in.");
			}			
			return false;
		}
	}

	public static boolean updateCheckOut() {
		try{
			SqlMapClient sqlMap = SqlMapConfig.getSqlMapInstance();	
			int u = sqlMap.update("updateCheckOut");
			if(u==1)
				return true;
			else{
				System.out.println("You didn't check in today.");
				return false;
			}
		}catch(SQLException e){
			System.out.println("An unexpected error happends checking you out.");
			return false;
		}
	}

	public static boolean updateLunchStart() {
		try{
			SqlMapClient sqlMap = SqlMapConfig.getSqlMapInstance();	
			int u = sqlMap.update("updateLunchStart");
			if(u==1)
				return true;
			else{
				System.out.println("You didn't check in today.");
				return false;
			}
		}catch(SQLException e){
			System.out.println("An unexpected error happends.");
			return false;
		}
	}

	public static boolean updateLunchEnd() {
		try{
			SqlMapClient sqlMap = SqlMapConfig.getSqlMapInstance();	
			int u = sqlMap.update("updateLunchEnd");
			if(u==1)
				return true;
			else{
				System.out.println("You didn't check in today.");
				return false;
			}
		}catch(SQLException e){
			System.out.println("An unexpected error happends.");
			return false;
		}
	}
	
	public static List<Times> getTimes(){
		List<Times> list ;
		
		try{
			SqlMapClient sqlMap = SqlMapConfig.getSqlMapInstance();	
			 list = sqlMap.queryForList("getTimes");
			 return list;
		}catch(SQLException e){
			System.out.println("An unexpected error happends.");
		}
		return null;
	}
	
}
